package org.eoti.io.xls;

/**
 * The MIT License
 * <p/>
 * Copyright (c) 2009 Malachi de AElfweald (malachid@gmail.com)
 * <p/>
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * <p/>
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
 * Software.
 * <p/>
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;

public class XLSReader
{
	protected POIFSFileSystem fs;
	protected HSSFWorkbook wb;
	protected HSSFSheet sheet;
	protected int sheetNum;
	protected ArrayList<XLSColumn> columns;
	protected ArrayList<Integer> rows;

	public XLSReader(File file)
			throws IOException
	{
		this(file, 0);
	}

	public XLSReader(File file, int sheetNum)
			throws IOException
	{
		fs = new POIFSFileSystem(new FileInputStream(file));
		wb = new HSSFWorkbook(fs);
		this.sheetNum = sheetNum;
		sheet = wb.getSheetAt(sheetNum);

		columns = new ArrayList<XLSColumn>();
		HSSFRow row = sheet.getRow(0);
		int lastCell = (int)row.getLastCellNum();
		for(int colNum=0; colNum<=lastCell; colNum++)
			columns.add(new XLSColumn(sheet, colNum));

		rows = new ArrayList<Integer>();
		reset();
	}

	public void reset()
	{
		rows.clear();
		int lastRow = sheet.getLastRowNum();
		for(int rowNum=1; rowNum<=lastRow; rowNum++)
			rows.add(rowNum);
	}

	public XLSColumn getColumn(String columnName)
	{
		int columnNum = indexOf(columnName);
		if(columnNum == -1) return null;
		return getColumn(columnNum);
	}

	public XLSColumn getColumn(int columnNum)
	{
		return columns.get(columnNum);
	}

	public int indexOf(String columnName)
	{
		// unfortunately, ArrayList.indexOf does String.equals(XLSColumn) so we can't use it
		for(XLSColumn column : columns)
		{
			if(column.columnTitle.equals(columnName)) return column.columnNum;
		}

		return -1;
	}

	public ArrayList<XLSColumn> columns(){return columns;}
	public ArrayList<Integer> rows(){return rows;}

	public void remove(HSSFRow row)
	{
		remove(row.getRowNum());
	}

	public void remove(int row)
	{
		rows.remove((Integer)row);// cast so we remove OBJECT not INDEX
	}

	public void removeAll(ArrayList<Integer> rows)
	{
		this.rows().removeAll(rows);
	}
}
